package com.ssm.controller.admin;

import com.alibaba.fastjson.JSONObject;
import com.ssm.controller.BaseController;
import com.ssm.entity.UmStudent;
import com.ssm.service.StudentService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
public class AdminStudentManageController extends BaseController {
    @Resource(name = "studentService")
    private StudentService studentService;

    // 查询
    @RequestMapping(value ="student/manage",method = RequestMethod.GET)
    public String StudentMageageQuery(HttpSession session, Map<String,Object> map){
        List<UmStudent> studentList=new ArrayList<>();
        logger.info("检查管理员权限");
        String adminId=checkAdmin(session);
        if (!adminId.equals("adminId")){
            return "redirect:/";
        }
        studentList=studentService.listAllStudent();
        map.put("studentList",studentList);
        for (int i = 0; i < studentList.size(); i++) {
            logger.info(studentList.get(i));
        }
        return "admin/studentManage";
    }
    //编辑功能
    @ResponseBody
    @RequestMapping(value = "student/edit",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    public String edit(HttpSession session, @RequestParam Integer studentid, @RequestParam String password, @RequestParam String email,@RequestParam String name,@RequestParam String gender,@RequestParam String major,@RequestParam String phonenumber ,@RequestParam String position){
        String adminId=checkAdmin(session);
        logger.info("检查权限");
        if (adminId.equals("adminId")){
            UmStudent umStudent=new UmStudent();
            umStudent.setStudentid(studentid);
            umStudent.setPassword(password);
            umStudent.setEmail(email);
            umStudent.setName(name);
            umStudent.setGender(gender);
            umStudent.setMajor(major);
            umStudent.setPhonenumber(phonenumber);
            umStudent.setPosition(position);
            Integer IsSuccuess=studentService.update(umStudent);
            JSONObject object=new JSONObject();
            if (IsSuccuess==1){
                logger.info("修改成功");

                object.put("status","success");
            }else{
                logger.info("修改错误");
                object.put("status","false");
            }
            return object.toJSONString();
        }else if (adminId.equals("StudentId")){
            UmStudent umStudent=new UmStudent();
            umStudent.setPassword(password);
            Integer IsSuccuess=studentService.update(umStudent);
            JSONObject object=new JSONObject();
            if (IsSuccuess==1){
                logger.info("学生信息修改成功");
                object.put("status","success");
            }else{
                logger.info("学生信息修改错误");
                object.put("status","false");
            }
            return object.toJSONString();
        }
        return "redirect:/";
    }
    //添加
    @ResponseBody
    @RequestMapping(value = "student/add",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    public String add(HttpSession session, @RequestParam Integer studentid, @RequestParam String password, @RequestParam String email,@RequestParam String name,@RequestParam String gender,@RequestParam String major,@RequestParam String phonenumber ,@RequestParam String position){
        logger.info("检查管理员权限");
        String adminId=checkAdmin(session);
        if (!adminId.equals("adminId")){
            return "redirect:/";
        }
        UmStudent umStudent=new UmStudent();
        umStudent.setStudentid(studentid);
        umStudent.setPassword(password);
        umStudent.setEmail(email);
        umStudent.setName(name);
        umStudent.setGender(gender);
        umStudent.setMajor(major);
        umStudent.setPhonenumber(phonenumber);
        umStudent.setPosition(position);
        Integer IsSuccuess=studentService.createStudent(umStudent);
        JSONObject object=new JSONObject();
        if (IsSuccuess==1){
            logger.info("添加成功");

            object.put("status","success");
        }else{
            logger.info("添加错误");
            object.put("status","false");
        }
        return object.toJSONString();
    }
    //删除
    @ResponseBody
    @RequestMapping(value = "student/delete",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    public String delete(HttpSession session,  @RequestParam Integer id){
        logger.info("检查管理员权限");
        String adminId=checkAdmin(session);
        if (!adminId.equals("adminId")){
            return "redirect:/";
        }
        Integer IsSuccess=studentService.deleteStudent(id);
        JSONObject object=new JSONObject();
        if (IsSuccess==1){
            logger.info("删除成功");

            object.put("status","success");
        }else{
            logger.info("删除错误");
            object.put("status","false");
        }
        return object.toJSONString();
    }

}
